/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2018. All rights reserved.
 * Description: Huawei safe print function define
 *
 * Huawei(R) HiNIC PCI Express Network Controller UEFI Driver.
 * This driver is only usable in the EFI Pre-boot execution environment.
 * All rights reserved.
 * Author	 :
 * Create	 : 2018/12/10
 */
#include "HwSafeOpInternal.h"
#include "HwSafePrint.h"

/*******************************************************************************
 * <FUNCTION DESCRIPTION>
 * Produces a Null-terminated Ascii string in an output buffer based on a Null-terminated
 * Ascii format string and variable argument list.
 * This function is similar as snprintf_s defined in C11.
 *
 * <INPUT PARAMETERS>
 *    bufferSize                  The size, in bytes, of the output buffer specified by StartOfBuffer.
 *    formatString                A Null-terminated Ascii format string.
 *
 * <OUTPUT PARAMETERS>
 *    startOfBuffer               A pointer to the output buffer for the produced Null-terminated Ascii string.
 *
 * <RETURN VALUE>
 *  The number of Ascii characters in the produced output buffer not including the Null-terminator.
 *********************************************************************************/
UINTN AsciiSPrintS(
    CHAR8          *startOfBuffer,
    UINTN          bufferSize,
    CONST CHAR8    *formatString,
    ...)
{
    VA_LIST marker;
    UINTN   numberOfPrinted;
    CHAR8   *pHeader = startOfBuffer;

    if ((startOfBuffer == NULL) || (bufferSize == 0) ||
        (bufferSize > EFI_SECUREC_ASCII_STRING_MAX_LEN)) {
        return 0;
    }
    if (formatString == NULL) {
        *pHeader = '\0';
        return 0;
    }

    VA_START(marker, formatString);
    numberOfPrinted = AsciiVSPrint(startOfBuffer, bufferSize, formatString,
                                   marker);
    VA_END(marker);
    return numberOfPrinted;
}

/*******************************************************************************
 * <FUNCTION DESCRIPTION>
 * Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
 * Unicode format string and variable argument list.
 * This function is similar as snprintf_s defined in C11.
 *
 * <INPUT PARAMETERS>
 *    bufferSize                  The size, in bytes, of the output buffer specified by StartOfBuffer.
 *    formatString                A Null-terminated Unicode format string.
 *
 * <OUTPUT PARAMETERS>
 *    startOfBuffer               A pointer to the output buffer for the produced Null-terminated Unicode string.
 *
 * <RETURN VALUE>
 *  The number of Unicode characters in the produced output buffer not including the Null-terminator.
 *********************************************************************************/
UINTN UnicodeSPrintS(
    CHAR16        *startOfBuffer,
    UINTN         bufferSize,
    CONST CHAR16  *formatString,
    ...)
{
    VA_LIST marker;
    UINTN   numberOfPrinted;
    CHAR16  *pHeader = startOfBuffer;

    if ((startOfBuffer == NULL) || (bufferSize == 0) ||
        (bufferSize > EFI_SECUREC_UNICODE_STRING_MAX_LEN*sizeof(CHAR16))) {
        return 0;
    }
    if (formatString == NULL) {
        *pHeader = L'\0';
        return 0;
    }

    VA_START(marker, formatString);
    numberOfPrinted = UnicodeVSPrint(startOfBuffer, bufferSize, formatString,
                                     marker);
    VA_END(marker);
    return numberOfPrinted;
}

/*******************************************************************************
 * <FUNCTION DESCRIPTION>
 * Produces a Null-terminated Ascii string in an output buffer based on a Null-terminated
 * Ascii format string and variable argument list.
 * This function is similar as snprintf_s defined in C11.
 *
 * <INPUT PARAMETERS>
 *    bufferSize                  The size, in bytes, of the output buffer specified by StartOfBuffer.
 *    formatString               A Null-terminated Ascii format string.
 *    maker                        Pointer to list of arguments.
 *
 * <OUTPUT PARAMETERS>
 *    startOfBuffer               A pointer to the output buffer for the produced Null-terminated Ascii string.
 *
 * <RETURN VALUE>
 *  The number of Ascii characters in the produced output buffer not including the Null-terminator.
 *********************************************************************************/
UINTN AsciiVSPrintS(
    CHAR8         *startOfBuffer,
    UINTN          bufferSize,
    CONST CHAR8   *formatString,
    VA_LIST        marker)
{
    if ((startOfBuffer == NULL) || (bufferSize == 0) ||
        (bufferSize > EFI_SECUREC_ASCII_STRING_MAX_LEN)) {
        return 0;
    }
    if (formatString == NULL) {
        *startOfBuffer = '\0';
        return 0;
    }

    return AsciiVSPrint(startOfBuffer, bufferSize, formatString, marker);
}

/*******************************************************************************
 * <FUNCTION DESCRIPTION>
 * Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
 * Unicode format string and variable argument list.
 * This function is similar as snprintf_s defined in C11.
 *
 * <INPUT PARAMETERS>
 *    bufferSize                  The size, in bytes, of the output buffer specified by startOfBuffer.
 *    formatString                A Null-terminated Unicode format string.
 *    maker                        Pointer to list of arguments.
 *
 * <OUTPUT PARAMETERS>
 *    startOfBuffer               A pointer to the output buffer for the produced Null-terminated Unicode string.
 *
 * <RETURN VALUE>
 *  The number of Unicode characters in the produced output buffer not including the Null-terminator.
 *********************************************************************************/
UINTN UnicodeVSPrintS(
    CHAR16         *startOfBuffer,
    UINTN           bufferSize,
    CONST CHAR16   *formatString,
    VA_LIST         marker)
{
    if ((startOfBuffer == NULL) || (bufferSize == 0) ||
        (bufferSize > EFI_SECUREC_UNICODE_STRING_MAX_LEN*sizeof(CHAR16))) {
        return 0;
    }
    if (formatString == NULL) {
        *startOfBuffer = L'\0';
        return 0;
    }

    return UnicodeVSPrint(startOfBuffer, bufferSize, formatString, marker);
}

#ifdef EDKII_SUPPORT
/*******************************************************************************
 * <FUNCTION DESCRIPTION>
 * Produces a Null-terminated Ascii string in an output buffer based on a Null-terminated
 * Ascii format string and a BASE_LIST argument list.
 *
 *  If bufferSize == 0 or bufferSize large than max length or startOfBuffer is NULL,
 *  the output buffer is unmodified and 0 is returned.
 *  If formatString is NULL, the output buffer set to zero and 0 is returned.
 *
 * <INPUT PARAMETERS>
 *    bufferSize                  The size, in bytes, of the output buffer specified by StartOfBuffer.
 *    formatString               A Null-terminated Ascii format string.
 *    maker                        Pointer to list of the variable arguments.
 *
 * <OUTPUT PARAMETERS>
 *    startOfBuffer               A pointer to the output buffer for the produced Null-terminated Ascii string.
 *
 * <RETURN VALUE>
 *  The number of Ascii characters in the produced output buffer not including the Null-terminator.
 *********************************************************************************/
UINTN AsciiBSPrintS(
    CHAR8         *startOfBuffer,
    UINTN          bufferSize,
    CONST CHAR8   *formatString,
    BASE_LIST      marker)
{
    if ((startOfBuffer == NULL) || (bufferSize == 0) ||
        (bufferSize > EFI_SECUREC_ASCII_STRING_MAX_LEN)) {
        return 0;
    }
    if (formatString == NULL) {
        *startOfBuffer = '\0';
        return 0;
    }

    return AsciiBSPrint(startOfBuffer, bufferSize, formatString, marker);
}

/*******************************************************************************
 * <FUNCTION DESCRIPTION>
 * Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated
 * Unicode format string and variable argument list.
 *
 *  If bufferSize == 0 or bufferSize large than max length or startOfBuffer is NULL,
 *  the output buffer is unmodified and 0 is returned.
 *  If formatString is NULL, the output buffer set to zero and 0 is returned.
 *
 * <INPUT PARAMETERS>
 *    bufferSize                  The size, in bytes, of the output buffer specified by startOfBuffer.
 *    formatString                A Null-terminated Unicode format string.
 *    maker                        Pointer to list of arguments.
 *
 * <OUTPUT PARAMETERS>
 *    startOfBuffer               A pointer to the output buffer for the produced Null-terminated Unicode string.
 *
 * <RETURN VALUE>
 *  The number of Unicode characters in the produced output buffer not including the Null-terminator.
 *********************************************************************************/
UINTN UnicodeBSPrintS(
    CHAR16         *startOfBuffer,
    UINTN           bufferSize,
    CONST CHAR16   *formatString,
    BASE_LIST       marker)
{
    if ((startOfBuffer == NULL) || (bufferSize == 0) ||
        (bufferSize > EFI_SECUREC_UNICODE_STRING_MAX_LEN*sizeof(CHAR16))) {
        return 0;
    }
    if (formatString == NULL) {
        *startOfBuffer = L'\0';
        return 0;
    }

    return UnicodeBSPrint(startOfBuffer, bufferSize, formatString, marker);
}
#endif
